﻿using System.Web.Mvc;

namespace dPet.Web.Hospital.Controllers
{
	using dPet.Application.DataObjects;
	using dPet.Application.QueryObjects;
	using dPet.Application.Services;
	using dPet.Web.Hospital.Fliters;
	using dPet.Web.Hospital.ViewModels;
	using Hangerd.Mvc;
	using Hangerd.Mvc.ViewModels;

	public class InventoryController : HangerdController
	{
		private const int BATCH_LIST_PAGE_SIZE = 15;
		private const int LORECORD_LIST_PAGE_SIZE = 15;
		private const int ALARM_LIST_PAGE_SIZE = 15;

		private readonly IInventoryService _invoicingService;

		public InventoryController(IInventoryService invoicingService)
		{
			_invoicingService = invoicingService;
		}

		#region InentoryLO

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.ProductManagement, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult ReportInentoryLO(string productId, string inventoryNumber, InventoryLOTypeDto loType, int quantity)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _invoicingService.ReportInventoryLO(productId, inventoryNumber, loType, quantity, loginStaff);

			return JsonContent(new
			{
				Value = result.Value,
				Message = result.Message
			});
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.InventoryBatchRecords)]
		public ActionResult InventoryLORecords(InventoryLORecordQueryDto queryDto, int pageIndex = 1)
		{
			var page = pageIndex < 1 ? 1 : pageIndex;
			var totalNumber = 0;
			var records = _invoicingService.GetInventoryLORecords(LoginStaffModel.Current.HospitalId, queryDto, page, LORECORD_LIST_PAGE_SIZE, out totalNumber);

			ViewBag.QueryDto = queryDto;

			return View(new PagedListModel<InventoryLORecordDto>
			{
				PageIndex = page,
				PageSize = LORECORD_LIST_PAGE_SIZE,
				TotalNumber = totalNumber,
				List = records
			});
		}

		#endregion

		#region Inventory

		[StaffLoginAuth(Privilege = PrivilegesDto.AddInventory)]
		public ActionResult AddInventory()
		{
			ViewBag.CookieName = string.Format("dpet_inv_{0}", LoginStaffModel.Current.Id);

			return View();
		}

		[HttpPost]
		[StaffLoginAuth(Privilege = PrivilegesDto.AddInventory, Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult AddInventory(InventoryBatchDetailDto[] details)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _invoicingService.AddInventoryByBatch(loginStaff.HospitalId, details, loginStaff);

			return JsonContent(new
			{
				Success = result.Value != null,
				Message = result.Message
			});
		}

		[StaffLoginAuth(Privilege = PrivilegesDto.InventoryBatchRecords)]
		public ActionResult InventoryBatchRecords(InventoryBatchQueryDto queryDto, int pageIndex = 1)
		{
			var page = pageIndex < 1 ? 1 : pageIndex;
			var totalNumber = 0;
			var batches = _invoicingService.GetInventoryBatches(LoginStaffModel.Current.HospitalId, queryDto, page, BATCH_LIST_PAGE_SIZE, out totalNumber);

			ViewBag.QueryDto = queryDto;

			return View(new PagedListModel<InventoryBatchDto>
			{
				PageIndex = page,
				PageSize = BATCH_LIST_PAGE_SIZE,
				TotalNumber = totalNumber,
				List = batches
			});
		}

		#endregion

		#region Alarm

		[StaffLoginAuth]
		public ActionResult AlarmList(int pageIndex = 1)
		{
			var page = pageIndex < 1 ? 1 : pageIndex;
			var totalNumber = 0;
			var alarms = _invoicingService.GetInventoryAlarms(LoginStaffModel.Current.HospitalId, page, ALARM_LIST_PAGE_SIZE, out totalNumber);

			return View(new PagedListModel<InventoryAlarmItemDto>
			{
				PageIndex = page,
				PageSize = ALARM_LIST_PAGE_SIZE,
				TotalNumber = totalNumber,
				List = alarms
			});
		}

		[HttpPost]
		[StaffLoginAuth(Ajax = true, DefaultAjaxResult = "{\"Success\":false,\"Message\":\"操作失败\"}")]
		public ActionResult DisableAlarm(string productId, InventoryAlarmTypeDto alarmType)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _invoicingService.DisableInventoryAlarm(productId, alarmType, loginStaff);

			return JsonContent(new
			{
				Success = result.Value,
				Message = result.Message
			});
		}

		#endregion
	}
}
